package d0331;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ArraysDemo {
    public static void main(String[] args) {
        Student[] students = new Student[5];

        for (int i = 0; i < students.length; i++) {
            Scanner in = new Scanner(System.in);
            System.out.println("输入第" + (i + 1) + "个学生学号");
            String id = in.nextLine();
            System.out.println("输入姓名");
            String name = in.nextLine();
            System.out.println("输入语文成绩");
            int chinese = in.nextInt();
            System.out.println("输入数学成绩");
            int math = in.nextInt();
            System.out.println("输入英语成绩");
            int english = in.nextInt();
            Student student = new Student();
            student.id = id;
            student.name = name;
            student.chinese = chinese;
            student.math = math;
            student.english = english;
            student.avg = (chinese + math + english) / 3;
            students[i] = student;
        }
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);
        }
        File file = new File("D:/student.txt");
        if (file.exists()) {
            System.out.println("文件存在");
        } else {
            System.out.println("文件不存在，正在创建....");
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileOutputStream fileout = null;
        OutputStreamWriter output = null;
        try {
            fileout = new FileOutputStream(file);
            output = new OutputStreamWriter(fileout, "gbk");
            output.write("学号\t" + "姓名\t" + "语文\t" + "数学\t" + "英语\t" + "平均值\t");
            for (int i = 0; i < students.length; i++) {
                output.write("\r\n" + students[i].id + "\t" + students[i].name + "\t" + students[i].chinese
                        + "\t" + students[i].math + "\t" + students[i].english + "\t" + students[i].avg);
                output.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
                fileout.close();
            } catch (Exception e) {
            }
        }


    }

}




